package cloud.maque.auth.handler.oauth2callback;

import cloud.maque.auth.annotations.ThirdLoginAnn;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class AbstractOAuth2ProcessorHolder<T> implements ApplicationContextAware {

    private ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }


    /**
     * 根据注解找到具体的类
     *
     * @param c
     * @return
     */
    protected T getProcessor(Class<T> c, String loginType) {
        Map<String, T> processorMap = applicationContext.getBeansOfType(c);
        List<T> allList = new ArrayList<>(processorMap.values());
        for (Integer i = 0; i < allList.size(); i++) {
            ThirdLoginAnn dataIndex = allList.get(i).getClass().getAnnotation(ThirdLoginAnn.class);
            if (dataIndex != null && dataIndex.value().name().equalsIgnoreCase(loginType)) {
                return allList.get(i);
            }
        }
        return null;
    }
}
